package com.swas.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swas.entity.BaseDevice;
import com.swas.entity.BaseStock;
import com.swas.service.BaseDeviceService;
import com.swas.util.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private BaseDeviceService baseDeviceService;

    @GetMapping("/findAll")//显示全部
    public ResultVO findAll(){
       List<BaseDevice> devices =  baseDeviceService.findAll();
       return ResultVO.success("ok",devices);
    }

    @DeleteMapping ("/deletOne")//根据ID删除一个
    public ResultVO deletOne ( Integer id){
        System.out.println(id);
        boolean flag = baseDeviceService.deletOne(id);
        if (flag){
            return  ResultVO.success("删除成功");
        }
        return  ResultVO .fail("删除失败");
    }
    @GetMapping("/getOne")
    public  ResultVO getOne(Integer id){
    BaseStock baseStock = baseDeviceService.getOne(id);
    return ResultVO.success(null,baseStock);
    }

    @GetMapping("/selectLike")
    public ResultVO selectLike(@Param("no") String no, @Param("name") String name, @Param("buyuser") String buyuser, @Param("checkuser") String checkuser ){
        List<BaseStock> devices =  baseDeviceService.selectLike(no, name, buyuser, checkuser);
        return ResultVO.success("ok",devices);
    }

//修改信息
    @PostMapping("/updateOne")
    public  ResultVO updateOne(@RequestBody BaseStock baseStock){
        boolean flag = baseDeviceService.updateOne(baseStock);
        if (flag){
            return ResultVO.success("修改成功");
        }
        return  ResultVO.fail("删除失败");
    }
    @RequestMapping("/selectBaseStockAll")
    public ResultVO selectBaseStockAll(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BaseStock> list = baseDeviceService.selectBaseStockAll();
        PageInfo<BaseStock> pageInfo = new PageInfo<>(list);
        return ResultVO.success("查询成功", pageInfo);
    }
}
